<html>
<head><meta charset="utf-8"><title>ptr::read vs ptr::write · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html">ptr::read vs ptr::write</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220587932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220587932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220587932">(Dec 21 2020 at 14:18)</a>:</h4>
<p>I just noticed that <code>ptr::read</code> is implemented as a library-only function with <code>copy_nonoverlapping</code>, while <code>ptr::write</code> uses an intrinsic. Is there a reason for this? <code>ptr::write</code> could also be implemented without an intrsic as something like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">write</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">dst</span>: <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">src</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="n">copy_nonoverlapping</span><span class="p">(</span><span class="o">&amp;</span><span class="n">src</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="n">dst</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">());</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="220599707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220599707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220599707">(Dec 21 2020 at 16:09)</a>:</h4>
<p>You also need to not drop <code>src</code></p>



<a name="220599902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220599902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220599902">(Dec 21 2020 at 16:11)</a>:</h4>
<p>I imagine the intrinsic was written before <code>ManuallyDrop</code> and <code>MaybeUninit</code> existed, nor even unions</p>



<a name="220603881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220603881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220603881">(Dec 21 2020 at 16:46)</a>:</h4>
<p><span class="user-mention silent" data-user-id="138448">cuviper</span> <a href="#narrow/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite/near/220599707">said</a>:</p>
<blockquote>
<p>You also need to not drop <code>src</code></p>
</blockquote>
<p>ah, good point.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">write</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">dst</span>: <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">src</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="n">src</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ManuallyDrop</span>::<span class="n">new</span><span class="p">(</span><span class="n">src</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="n">copy_nonoverlapping</span><span class="p">(</span><span class="o">&amp;*</span><span class="n">src</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="n">dst</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">());</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="220603913"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220603913" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220603913">(Dec 21 2020 at 16:46)</a>:</h4>
<p><span class="user-mention silent" data-user-id="138448">cuviper</span> <a href="#narrow/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite/near/220599902">said</a>:</p>
<blockquote>
<p>I imagine the intrinsic was written before <code>ManuallyDrop</code> and <code>MaybeUninit</code> existed, nor even unions</p>
</blockquote>
<p>yeah, this is pre-1.0 material. But <code>read</code> clearly was updated since then, so I wondered if there is a reason that <code>write</code> was not.</p>



<a name="220603964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220603964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220603964">(Dec 21 2020 at 16:47)</a>:</h4>
<p>That is a lot more code for LLVM to churn through and is much slower in debug mode and with cg_clif.</p>



<a name="220608166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220608166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220608166">(Dec 21 2020 at 17:30)</a>:</h4>
<p><code>read</code> used to get by with <code>mem:: uninitialized()</code>, however bad that was, and you changed that in <a href="https://github.com/rust-lang/rust/issues/54668">#54668</a>.</p>



<a name="220608373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220608373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220608373">(Dec 21 2020 at 17:32)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> if there's a codegen benefit to intrinsic <code>write</code>, I wonder if we might also benefit from an intrinsic <code>read</code>.</p>



<a name="220608454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220608454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220608454">(Dec 21 2020 at 17:33)</a>:</h4>
<p>Probably</p>



<a name="220608601"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220608601" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220608601">(Dec 21 2020 at 17:34)</a>:</h4>
<p>An alternative I have been thinking about for a while would be to introduce something like <code>#[inline(force)]</code> that will force MIR-inlining and give a cycle error when there is an inlining cycle. This can then be applied to certain function that are guaranteed to not cause an inlining cycle.</p>



<a name="220609088"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220609088" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220609088">(Dec 21 2020 at 17:39)</a>:</h4>
<p>relying on optimisation just pushes the responsibility away and still incurs the cost of having to optimise the thing, much like using <code>MaybeUninit</code> would.</p>



<a name="220609234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220609234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220609234">(Dec 21 2020 at 17:40)</a>:</h4>
<p>Intrinsics are nice because some of them are converted to MIR by an unconditional pass.</p>



<a name="220609265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220609265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220609265">(Dec 21 2020 at 17:40)</a>:</h4>
<p>Maybe inlining would help, but I think <code>read</code> and <code>write</code> really are special. The compiler knows how to deal with initializing or moving from storage without the type shenanigans.</p>



<a name="220609683"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220609683" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220609683">(Dec 21 2020 at 17:44)</a>:</h4>
<p><code>#[inline(force)]</code> would be unconditional and not just an optional optimization pass. In addition any MIR passes are done before monomorphization, so it would only incur a tiny cost during compilation of libcore itself. All downstream users would just read the optimized MIR without any calls to <code>ManuallyDrop::new</code> and <code>mem::copy_nonoverlapping</code>. It wouldn't be as optimized as a dedicated intrinsic though, but still much better.</p>



<a name="220609726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220609726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220609726">(Dec 21 2020 at 17:45)</a>:</h4>
<p><code>#[inline(force)]</code> would also be useful for wrappers that only call a single intrinsic like <code>mem::size_of</code>.</p>



<a name="220609903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220609903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220609903">(Dec 21 2020 at 17:46)</a>:</h4>
<p>LLVM still has codegen issues with both our <code>ptr::read()</code> <a href="https://github.com/rust-lang/rust/issues/73258">#73258</a> and some uses of <code>MaybeUninit</code> compared to <code>mem::uninitialized()</code> like <a href="https://github.com/rust-lang/rust/issues/74267">#74267</a></p>



<a name="220681314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220681314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220681314">(Dec 22 2020 at 11:45)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite/near/220603964">said</a>:</p>
<blockquote>
<p>That is a lot more code for LLVM to churn through and is much slower in debug mode and with cg_clif.</p>
</blockquote>
<p>you mean because of the <code>ManuallyDrop</code>? The <code>copy_nonoverlapping</code> must be pretty close to what the intrinsic lowers to...</p>



<a name="220681863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220681863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220681863">(Dec 22 2020 at 11:54)</a>:</h4>
<p>That and the fact that <code>ptr::copy_nonoverlapping</code> is a wrapper around <code>intrinsics::copy_nonoverlapping</code>, both of which prevents optimizations when inlining is not done.</p>



<a name="220682182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220682182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220682182">(Dec 22 2020 at 11:59)</a>:</h4>
<p>FWIW, <code>write_unaligned</code> is already implemented like this</p>



<a name="220682229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220682229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220682229">(Dec 22 2020 at 12:00)</a>:</h4>
<p>and it seems strange to treat <code>ptr::read</code> and <code>ptr::write</code> so differently</p>



<a name="220682326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220682326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220682326">(Dec 22 2020 at 12:01)</a>:</h4>
<p>and without inlining, there's not much optimizing happening here anyway, since it means <code>ptr::write</code> itself will also not be inlined</p>



<a name="220682454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220682454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220682454">(Dec 22 2020 at 12:03)</a>:</h4>
<p>we could possibly use <code>mem::forget</code> instead of <code>ManuallyDrop</code> (that's what <code>write_unaligned</code> does, anyway). in this particular case, "using" src again after the copy seems fine (so the usual concern around <code>mem::forget</code> does not apply)</p>



<a name="220682465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220682465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220682465">(Dec 22 2020 at 12:03)</a>:</h4>
<p><code>ptr::write</code> could be implemented in just a few instructions (depending on the type size) but lack of inlining currently makes it several times as big.</p>



<a name="220682587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220682587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220682587">(Dec 22 2020 at 12:05)</a>:</h4>
<p>It is true that this by far isn't the only case where manual inlining would have a benefit. That is why I would like a <code>#[inline(force)]</code> for MIR inlining.</p>



<a name="220682874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220682874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220682874">(Dec 22 2020 at 12:09)</a>:</h4>
<p>Let's see what perf says: <a href="https://github.com/rust-lang/rust/pull/80290">https://github.com/rust-lang/rust/pull/80290</a></p>



<a name="220682940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220682940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220682940">(Dec 22 2020 at 12:10)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite/near/220681863">said</a>:</p>
<blockquote>
<p>That and the fact that <code>ptr::copy_nonoverlapping</code> is a wrapper around <code>intrinsics::copy_nonoverlapping</code></p>
</blockquote>
<p>If this is a concern, we can make this a direct intrinsic call again (looks like we'll lose the debug assertions for now anyway, due to <a href="https://github.com/rust-lang/rust/pull/79684">https://github.com/rust-lang/rust/pull/79684</a>)</p>



<a name="220682977"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220682977" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220682977">(Dec 22 2020 at 12:10)</a>:</h4>
<p>Most of the perf gain will be for runtime of debug mode executables.</p>



<a name="220682987"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220682987" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220682987">(Dec 22 2020 at 12:10)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite/near/220682587">said</a>:</p>
<blockquote>
<p>It is true that this by far isn't the only case where manual inlining would have a benefit. That is why I would like a <code>#[inline(force)]</code> for MIR inlining.</p>
</blockquote>
<p>FWIW I think this would also help for some of the slice iterator code, where we're using macros because function calls cause a notable perf hit even in optimized code (inlining happening too late, or so)</p>



<a name="220683051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220683051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220683051">(Dec 22 2020 at 12:11)</a>:</h4>
<p>Yes, I tried manually replacing <code>mem::size_of</code> with <code>intrinsics::size_of</code> in the slice iterator code and it caused a few percent improvement on simple-raytracer compiled with cg_clif.</p>



<a name="220683172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220683172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220683172">(Dec 22 2020 at 12:13)</a>:</h4>
<p>oO</p>



<a name="220683249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/220683249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#220683249">(Dec 22 2020 at 12:14)</a>:</h4>
<p>The improvements for cg_clif will be a bit better than cg_llvm as cranelift currently always emits a function prologue and epilogue but still.</p>



<a name="221594443"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/221594443" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Erick Tryzelaar <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#221594443">(Jan 04 2021 at 23:19)</a>:</h4>
<p><code>#[inline(force)]</code> could also be nice for my <code>Box::new</code> optimization. As I mentioned in the other thread, it might be expedient to turn <code>Box::new</code> into an intrinsic to make sure that <code>Box:new([0; 4096*4096])</code> is guaranteed to never blow the stack, it would be painful to intrinsic-ify all the other functions/methods that essentially do the same thing, like <code>Box::pin</code></p>



<a name="221595089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/221595089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#221595089">(Jan 04 2021 at 23:28)</a>:</h4>
<p>Should this happen at the regular inlining pass, or should this be something that happens really early in the pipeline, essentially treating the inlined body as manually inlined so even various analyses like borrowck will not see the difference?</p>



<a name="221595260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/221595260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#221595260">(Jan 04 2021 at 23:30)</a>:</h4>
<p>I don't think we'd want it to affect borrow ck (or really any similar pass), but doing it before LLVM would be critical I imagine.</p>



<a name="221596882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/221596882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#221596882">(Jan 04 2021 at 23:49)</a>:</h4>
<p>ok, then we can indeed do it in the regular inlining pass. I guess <code>#[inline(force)]</code> can cause query cycle errors if you try to inline recursive (even mutually) functions. What's the difference to <code>#[inline(always)]</code> for that matter? I always thought that should already <em>always</em> inline and we are just missing the MIR opt to do it (though LLVM's inline-always does not actually always inline)</p>



<a name="221607210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/221607210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#221607210">(Jan 05 2021 at 02:30)</a>:</h4>
<p>I could imagine <code>inline(rustc_mir_force)</code> that's never-stable and just always runs the mir inliner on it, trusting libs-impl to put it on the right things.  That could potentially stabilize sooner than the generic one.</p>
<p>Alternatively, <code>inline(force)</code> could give an error if for some reason it can't be inlined, such as if it's recursive, which <code>inline(always)</code> probably can't.  Or it could apply in debug mode too (I don't remember whether <code>inline(always)</code> does anything in non-release).</p>



<a name="221614739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/221614739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> LingM <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#221614739">(Jan 05 2021 at 05:37)</a>:</h4>
<p>I've certainly seen people using <code>inline(always)</code> on functions that get inlined anyway specifically because they wanted them inlined in debug mode, too.</p>



<a name="221619473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/221619473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#221619473">(Jan 05 2021 at 07:31)</a>:</h4>
<p>(Err, I said "stabilize sooner" in my previous message, but meant "happen sooner", since I also said "never-stable", oops.)</p>



<a name="221623154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/221623154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#221623154">(Jan 05 2021 at 08:36)</a>:</h4>
<p>What would be the examples that are not MIR inlined but should be?</p>



<a name="221626868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/221626868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#221626868">(Jan 05 2021 at 09:28)</a>:</h4>
<blockquote>
<p>Alternatively, inline(force) could give an error if for some reason it can't be inlined, such as if it's recursive, which inline(always) probably can't. Or it could apply in debug mode too (I don't remember whether inline(always) does anything in non-release).</p>
</blockquote>
<p>Both are exactly what I want. Inlining cycles should error and I think it should inline in debug mode. <code>#[inline(force)]</code> should mostly be used for functions where a function call requires as much or more instructions as the body of the function to be inlined and as such inlining would be expected to not regress compile times.</p>



<a name="221714651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/ptr%3A%3Aread%20vs%20ptr%3A%3Awrite/near/221714651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite.html#221714651">(Jan 05 2021 at 23:14)</a>:</h4>
<p>yes, performing the inline even in debug mode is crucial here</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>